﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OracleClient;
namespace BenefitHelper.Data.ServerData
{
    /// <summary>
    /// 持仓明细表
    /// </summary>
    public class T_PositionDetail_History
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 帐户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 合约
        /// </summary>
        public string InstrumentId { get; set; }
        /// <summary>
        /// 经纪公司代码
        /// </summary>
        public string Brokerid { get; set; }
        /// <summary>
        /// 投资者代码
        /// </summary>
        public string Investorid { get; set; }
        /// <summary>
        /// 投保
        /// </summary>
        public string HedgeFlag { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public string Direction { get; set; }
        /// <summary>
        /// 开仓时间
        /// </summary>
        public int OpenTime { get; set; }
        /// <summary>
        /// 开仓日期
        /// </summary>
        public int OpenDate { get; set; }
        /// <summary>
        /// 成交编号
        /// </summary>
        public string TradeId { get; set; }
        /// <summary>
        /// 数量
        /// </summary>
        public int Volume { get; set; }
        /// <summary>
        /// 开仓价
        /// </summary>
        public double OpenPrice { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public string TradingDay { get; set; }
        /// <summary>
        /// 结算编号
        /// </summary>
        public int SettleMentId { get; set; }
        /// <summary>
        /// 成交类型
        /// </summary>
        public string TradeType { get; set; }
        /// <summary>
        /// 组合合约代码
        /// </summary>
        public string CombinStrumentId { get; set; }
        /// <summary>
        /// 交易所id
        /// </summary>
        public string ExchangeId { get; set; }
        /// <summary>
        /// 逐日盯市平仓盈亏
        /// </summary>
        public double CloseProfitByDate { get; set; }
        /// <summary>
        /// 逐笔对冲平仓盈亏
        /// </summary>
        public double CloseProfitByTrade { get; set; }
        /// <summary>
        /// 逐日盯市持仓盈亏
        /// </summary>
        public double PositionProfitByDate { get; set; }
        /// <summary>
        /// 逐笔对冲持仓盈亏
        /// </summary>
        public double PositionProfitByTrade { get; set; }
        /// <summary>
        /// 保证金
        /// </summary>
        public double Margin { get; set; }
        /// <summary>
        /// 交易所保证金
        /// </summary>
        public double Exchmargin { get; set; }
        /// <summary>
        /// 保证金率
        /// </summary>
        public double MarginRateByMoney { get; set; }
        /// <summary>
        /// 保证金率(按手数)
        /// </summary>
        public double MarginRateByVolume { get; set; }
        /// <summary>
        /// 昨结算
        /// </summary>
        public double YesterdaySettlement { get; set; }
        /// <summary>
        /// 结算价
        /// </summary>
        public double SettleMentPrice { get; set; }
        /// <summary>
        /// 平仓量
        /// </summary>
        public int CloseVolume { get; set; }
        /// <summary>
        /// 平仓金额
        /// </summary>
        public double CloseAmount { get; set; }
        /// <summary>
        /// 品种
        /// </summary>
        public string ProductId { get; set; }
        /// <summary>
        /// 交割期
        /// </summary>
        public string DeliveryDate { get; set; }
        /// <summary>
        /// 浮动盈亏
        /// </summary>
        public double FloatingProfit { get; set; }
        /// <summary>
        /// 盯市盈亏
        /// </summary>
        public double PositionProfit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Commission { get; set; }
        /// <summary>
        /// 更新日期
        /// </summary>
        public int UpdateDate { get; set; }



        /// <summary>
        /// 查询结算后的持仓明细情况
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        private System.Collections.Generic.List<T_PositionDetail_History> GetInfo(string pdate)
        {
            List<T_PositionDetail_History> tph = new List<T_PositionDetail_History>();
            T_Settle_History tsh = new T_Settle_History();
            int settlementid = tsh.GetDayMaxSettlementInfoId(pdate);
            if (settlementid != -1)
            {
                tph = GetPositionDetailHistory(settlementid, pdate);
            }
            return tph;
        }

        /// <summary>
        /// 查询某天最大结算编号的资金记录
        /// </summary>
        /// <param name="settlementid">结算编号</param>
        /// <param name="pdate">日期</param>
        /// <returns></returns>
        private System.Collections.Generic.List<T_PositionDetail_History> GetPositionDetailHistory(int settlementid, string pdate)
        {
            System.Collections.Generic.List<T_PositionDetail_History> tsm = new System.Collections.Generic.List<T_PositionDetail_History>();
            string sql = "select * from T_PositionDetail_History where SettlementId='" + settlementid + "' and tradingday='" + pdate + "'";
            sql.Debug();
            OracleDataReader sdr = Common.ADO.Tools.OracleHelper.ExecuteReader(sql);
            while (sdr.Read())
            {
                if (Convert.ToInt32(sdr["volume"]) > 0)
                {
                    T_PositionDetail_History s = new T_PositionDetail_History();
                    s.AccountId = Convert.ToInt32(sdr["accountid"]);
                    s.Brokerid = Convert.ToString(sdr["Brokerid"]);
                    s.CloseAmount = Convert.ToDouble(sdr["CloseAmount"]);
                    s.CloseProfitByDate = Convert.ToDouble(sdr["CloseProfitByDate"]);
                    s.CloseProfitByTrade = Convert.ToDouble(sdr["CloseProfitByTrade"]);
                    s.CloseVolume = Convert.ToInt32(sdr["CloseVolume"]);
                    s.CombinStrumentId = Convert.ToString(sdr["CombinStrumentId"]);
                    s.Commission = Convert.ToDouble(sdr["Commission"]);
                    s.DeliveryDate = Convert.ToString(sdr["DeliveryDate"]);
                    s.Direction = Convert.ToString(sdr["Direction"]);
                    s.ExchangeId = Convert.ToString(sdr["ExchangeId"]);
                    s.Exchmargin = Convert.ToDouble(sdr["Exchmargin"]);
                    s.FloatingProfit = Convert.ToDouble(sdr["FloatingProfit"]);
                    s.HedgeFlag = Convert.ToString(sdr["HedgeFlag"]);
                    s.Id = Convert.ToInt32(sdr["Id"]);
                    s.InstrumentId = Convert.ToString(sdr["InstrumentId"]);
                    s.Investorid = Convert.ToString(sdr["Investorid"]);
                    s.Margin = Convert.ToDouble(sdr["Margin"]);
                    s.MarginRateByMoney = Convert.ToDouble(sdr["MarginRateByMoney"]);
                    s.MarginRateByVolume = Convert.ToDouble(sdr["MarginRateByVolume"]);
                    s.OpenDate = Convert.ToInt32(sdr["OpenDate"]);
                    s.OpenPrice = Convert.ToDouble(sdr["OpenPrice"]);
                    s.OpenTime = Convert.ToInt32(sdr["OpenTime"]);
                    s.PositionProfit = Convert.ToDouble(sdr["PositionProfit"]);
                    s.PositionProfitByDate = Convert.ToDouble(sdr["PositionProfitByDate"]);
                    s.PositionProfitByTrade = Convert.ToDouble(sdr["PositionProfitByTrade"]);
                    s.ProductId = Convert.ToString(sdr["ProductId"]);
                    s.SettleMentId = Convert.ToInt32(sdr["SettleMentId"]);
                    s.SettleMentPrice = Convert.ToDouble(sdr["SettleMentPrice"]);
                    s.TradeId = Convert.ToString(sdr["TradeId"]);
                    s.TradeType = Convert.ToString(sdr["TradeType"]);
                    s.TradingDay = Convert.ToString(sdr["TradingDay"]);
                    s.UpdateDate = Convert.ToInt32(sdr["UpdateDate"]);
                    s.Volume = Convert.ToInt32(sdr["Volume"]);
                    s.YesterdaySettlement = Convert.ToDouble(sdr["YesterdaySettlement"]);
                    tsm.Add(s);
                }
            }
            sdr.Close();
            return tsm;
        }

        /// <summary>
        /// 每天都需要执行的把Server中的数据导入到分析系统中的方法
        /// </summary>
        /// <param name="pdate">日期</param>
        public void CopyServerPositionDetailHistoryDataIntoBenefit(string pdate, DB.DBManager db)
        {
            Data.ServerData.T_PositionDetail_History h = new Data.ServerData.T_PositionDetail_History();
            List<Data.ServerData.T_PositionDetail_History> tsm = h.GetInfo(pdate);
            foreach (Data.ServerData.T_PositionDetail_History ha in tsm)
            {
                db.PositionDetailHistory.Add(ha);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 删除某日的所有持仓明细记录
        /// </summary>
        /// <param name="pdate"></param>
        public void DeleteDayPositionDetailHistoryFromBenefit(string pdate, DB.DBManager db)
        {
            var historys = from t in db.PositionDetailHistory where t.TradingDay.Equals(pdate) select t;
            foreach (Data.ServerData.T_PositionDetail_History ha in historys)
            {
                db.PositionDetailHistory.Remove(ha);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 得到某日某合约的保证金
        /// </summary>
        /// <param name="accountId"></param>
        /// <param name="tradingDay"></param>
        /// <param name="instrumentid"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public double GetAccountInstrument(int accountId, string tradingDay, string instrumentid, DB.DBManager db)
        {
            double allSum = 0;
            var query = db.PositionDetailHistory.Where(a => a.AccountId == accountId).Where(a => a.TradingDay.Equals(tradingDay)).Where(a => a.InstrumentId.Equals(instrumentid));
            foreach (T_PositionDetail_History h in query)
            {
                allSum += h.Margin;
            }
            return allSum;
        }


        /// <summary>
        /// 得到某日某品种的保证金
        /// </summary>
        /// <param name="accountId"></param>
        /// <param name="tradingDay"></param>
        /// <param name="instrumentid"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public double GetAccountProduct(int accountId, string tradingDay, string productId, DB.DBManager db)
        {
            double allSum = 0;
            var query = db.PositionDetailHistory.Where(a => a.AccountId == accountId).Where(a => a.TradingDay.Equals(tradingDay)).Where(a => a.ProductId.Equals(productId));
            foreach (T_PositionDetail_History h in query)
            {
                allSum += h.Margin;
            }
            return allSum;
        }
    }
}